package cn.fl.busConsult.service.impl;

import java.util.ArrayList;
import java.util.List;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.fl.busConsult.service.IPrjPrdQuatBusService;
import cn.fl.busConsult.vo.PrjConsultBusPrpsRecordVO;
import cn.fl.commons.result.ResponseData;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.preloan.IPrjBusConsultApiService;
import cn.fl.preloan.condition.PrjConsultBusPrpsRecordQueryCondition;
import cn.fl.preloan.dto.ListInsurancePolicyResponseDTO;
import cn.fl.preloan.dto.prjBusConsult.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.Pager;


/**
 * @Description: 客车咨询报价主表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjPrdQuatBusServiceImpl implements IPrjPrdQuatBusService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(PrjPrdQuatBusServiceImpl.class);

	@Autowired
	private IPrjBusConsultApiService prjBusConsultApiService;

	@Override
	public Pager selectConsultRecordList(Pager page) throws BaseException {
		PrjConsultBusPrpsRecordQueryCondition condition = (PrjConsultBusPrpsRecordQueryCondition)page.getCondition();
		ResponseData<ListPrjConsultBusPrpsRecordResponseDTO> responseData = prjBusConsultApiService.prjConsultBusPrpsRecordList(page.getCurrentPage(),page.getEveryPage(),condition, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
		BaseLogUtils.info(logger,"selectConsultRecordList","调用贷前服务-查询客车咨询记录返回数据：" + (responseData == null ? "" : JSONObject.toJSONString(responseData)));
		if(responseData != null && CollectionUtils.isNotEmpty(responseData.getData().getPrjConsultBusPrpsRecordDTOS())){
			List<PrjConsultBusPrpsRecordDTO> prjConsultBusPrpsRecordDTOS = responseData.getData().getPrjConsultBusPrpsRecordDTOS();
			List<PrjConsultBusPrpsRecordVO> prjConsultBusPrpsRecordVOS = new ArrayList<>();
			for (PrjConsultBusPrpsRecordDTO prjConsultBusPrpsRecordDTO : prjConsultBusPrpsRecordDTOS) {
				PrjConsultBusPrpsRecordVO prjConsultBusPrpsRecordVO = new PrjConsultBusPrpsRecordVO();
				BeanUtils.copyProperties(prjConsultBusPrpsRecordDTO,prjConsultBusPrpsRecordVO);
				prjConsultBusPrpsRecordVOS.add(prjConsultBusPrpsRecordVO);
			}
			page.setTotalCount(responseData.getData().getTotal());
			page.setResultList(prjConsultBusPrpsRecordVOS);
		}
		return page;
	}

	@Override
	public DataResultVO exportBusConsultList(PrjConsultBusPrpsRecordQueryCondition qc) throws BaseException {
		ResponseData responseData = this.prjBusConsultApiService.exportPrjConsultBusPrpsRecordInfo(qc);
		BaseLogUtils.info(logger,"exportBusConsultList","导出客车咨询记录记录返回结果:"+ JSON.toJSONString(responseData));
		if (responseData != null && responseData.getCode() != null && responseData.getCode().equals(ResponseData.SUCCESS_CODE)) {
			BaseLogUtils.info(logger,"exportBusConsultList","导出客车咨询记录成功:"+JSON.toJSONString(qc));
			return new DataResultVO(BaseConstant.IS_YESNO_YES, "导出完成", responseData.getData());
		}
		BaseLogUtils.info(logger,"exportBusConsultList","导出客车咨询记录记录失败:"+JSON.toJSONString(qc));
		return new DataResultVO(BaseConstant.IS_YESNO_NO, "导出客车咨询记录失败");
	}

}
